//
//  VideoProgressSlider.m
//  18Birdies
//
//  Created by weihua on 2018/5/22.
//  Copyright © 2018年 Lc. All rights reserved.
//

#import "VideoProgressSlider.h"
#import "LcFoundation.h"

@interface VideoProgressSlider ()

@property(nonatomic, weak) IBOutlet UIProgressView* progressView;
@property(nonatomic, weak) IBOutlet UIImageView* slideIndicator;

@property(nonatomic, weak) IBOutlet UIView* sliderValueView;
@property(nonatomic, weak) IBOutlet NSLayoutConstraint* sliderValueWidth;

@end

@implementation VideoProgressSlider

- (instancetype)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    [self setupUI];
  }
  return self;
}

- (void)awakeFromNib {
  [super awakeFromNib];
  [self setupUI];
}

- (void)setupUI {
  self.sliderValueView.backgroundColor = LC_COLOR_BLUE;
  self.slideIndicator.image = [UIImage imageWithColor:LC_COLOR_WHITE];
  self.slideIndicator.layer.cornerRadius = self.slideIndicator.width / 2;
  self.slideIndicator.layer.masksToBounds = YES;
  [self clearMaxDragValue];
}

- (void)setEnabled:(BOOL)enabled {
  _enabled = enabled;
  self.userInteractionEnabled = enabled;
}

- (void)setValue:(float)value {
  LC_CHECK_TRUE(value >= 0 && value <= 1);
  _value = value;
  [self refreshIndicatorPositionWithValue:value];
}

- (void)setCachePercentage:(float)cachePercentage {
  _cachePercentage = cachePercentage;
  self.progressView.progress = cachePercentage;
}

- (void)setMaxDragValue:(float)maxDragValue {
  if (maxDragValue > 1.0) {
    maxDragValue = 1.0;
  }
  _maxDragValue = maxDragValue;
}

- (void)clearMaxDragValue {
  self.maxDragValue = CGFLOAT_MAX;
}

- (void)refreshIndicatorPositionWithValue:(float)value {
  float xCenter = (self.width - self.slideIndicator.width) * value + self.slideIndicator.width / 2;
  self.sliderValueWidth.constant = xCenter;
  [self layoutIfNeeded];
}

- (CGFloat)valueWithPosition:(CGPoint)position {
  CGFloat value = position.x / (self.width - self.slideIndicator.width);
  return MIN(MAX(value, 0), 1);
}

- (void)layoutSubviews {
  [super layoutSubviews];
  [self refreshIndicatorPositionWithValue:self.value];
}

- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
  self->_isTouching = YES;
  [self setDragValue:[self valueWithPosition:[touches.toArray[0] locationInView:self]]];
  [self.delegate didProgressSliderValueChanged:self];
}

- (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
  self->_isTouching = YES;
  [self setDragValue:[self valueWithPosition:[touches.toArray[0] locationInView:self]]];
  [self.delegate didProgressSliderValueChanged:self];
}

- (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
  self->_isTouching = NO;
  [self setDragValue:[self valueWithPosition:[touches.toArray[0] locationInView:self]]];
  [self.delegate didProgressSliderValueChanged:self];
}

- (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
  self->_isTouching = NO;
  [self.delegate didProgressSliderValueChanged:self];
}

- (void)setDragValue:(float)value {
  if (value > _maxDragValue) {
    return;
  }
  self.value = value;
}

@end
